package com.aaa.sbm.controller;

import com.aaa.sbm.entity.Dept;
import com.aaa.sbm.service.DeptService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.aaa.sbm.util.ResultStatus;
import com.aaa.sbm.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.executor.Executor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @FileName: DeptController
 * @Description:
 * @Author: zhz
 * @CreateTime: 2025/8/21 11:29
 * @Version: 1.0.0
 */
//@Controller
@RestController  // 相当于@Controller+@ResponseBody  该控制器中的所有方法默认都返回json
@RequestMapping("dept")  //父路径映射
@Log4j2
//@Scope("prototype")
public class DeptController {


    //private Logger log = LoggerFactory.getLogger(DeptController.class);

    public DeptController() {
        System.out.println("DeptController实例化------------------------");
    }

    //依赖注入
    //@Resource
    @Autowired
    private DeptService deptService;

    //private  static  final  Integer CODE=200;

    /**
     * 根据参数分页查询
     * @param page111
     * @return
     */
    //RequestBody 把json {pageNo:1,pageSize:5,data:{deptNo:101,deptName:'开发部'...}}字符串转换为对象
    @PostMapping("queryByPage") //在页面的ajax请求时，get请求无法携带body，如果带就会报HttpMessageNotReadableException
     //把返回的对象转换json字符串  Result --->{"code":200,"message":'执行成功',data:{list:[{deptNo:101,deptName:'开发部'..},{deptNo:101,deptName:'开发部'..},]}}
    //@ResponseBody
    public  Result  queryByPage(@RequestBody Page<Dept> page111){
        log.info("进入了queryByPage方法--------------------------------------");
       /* PageInfo pageInfo = null;
        try {
            //执行查询
            pageInfo = deptService.queryByPage(page);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return ResultUtil.error(null);*/
       // System.out.println(page111);

        Result result = ResultUtil.success(deptService.queryByPage(page111));
        log.info("queryByPage方法执行结束--------------------------------------");
        //执行查询,并封装返回
        return result;

    }
    /**
     * 根据编号查询对象
     * @param deptNo
     * @return
     */
    //@RequestMapping("queryById")
    @GetMapping("queryById")
    //@ResponseBody
    public Result queryById(Integer deptNo) {
        log.info("进入了queryById方法--------------------------------------");
        Dept dept = deptService.queryById(deptNo);
        //
//        Runnable runnable = new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("多线程开始运行！");
//            }
//        };
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
//        executorService.execute(runnable);

//        ExecutorService executorService1 = Executors.newFixedThreadPool(5);
//        executorService1.execute(()->{System.out.println("多线程开始运行！");});
//        if(dept==null){
//            return ResultUtil.error("部门查询失败");
//        }
        log.info("queryById方法执行结束--------------------------------------");
        return ResultUtil.success(dept);
    }

    /**
     * 添加
     * @param dept
     * @return
     */
    //@RequestMapping("add")
    @PostMapping("add")
    //@ResponseBody
    public Result add(@RequestBody Dept dept) {
        //int i = deptService.add(dept);
        //判断
//        if(i>0) {
//            return ResultUtil.success(i);
//        }
//        return ResultUtil.error("部门添加失败");
        return ResultUtil.success(deptService.add(dept));
    }

    /**
     * 更新
     * @param dept
     * @return
     */
    //@RequestMapping("update")
    @PostMapping("update")
    //@ResponseBody
    public Result update(@RequestBody Dept dept) {
        int i = deptService.update(dept);
//        //判断
//        if(i>0) {
//            return ResultUtil.success(i);
//        }
//        return ResultUtil.error("部门操作失败");
        return ResultUtil.success(i);
    }

    /**
     * 根据编号删除
     * @param deptNo
     * @return
     */
    //@RequestMapping("deleteById")
    @DeleteMapping("deleteById")
    //@ResponseBody
    public Result deleteById(Integer deptNo) {
        int i = deptService.deleteById(deptNo);
        //判断
//        if(i>0) {
//            return ResultUtil.success(i);
//        }
//        return ResultUtil.error("部门操作失败");
        return ResultUtil.success(i);
    }
}
